package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cndw.kungfu.comm.AccessClose;
import com.cndw.kungfu.comm.tencent.LogFieldType;
import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsDailyItem;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsTask;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.domain.Script;
import com.cndw.kungfu.domain.Undercity;
import com.cndw.kungfu.event.once.AiUndercityTask;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.StringProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.ModeSceneWall;
import com.cndw.kungfu.model.battle.AiReport;
import com.cndw.kungfu.model.battle.Team;
import com.cndw.kungfu.model.goods.PlayerBoxs;
import com.cndw.kungfu.model.goods.domain.SubPops;
import com.cndw.kungfu.model.goods.domain.SysModeAwards;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.model.undercity.PlayerUndercity;
import com.cndw.kungfu.vo.PassThroughRecordVO;
import com.cndw.kungfu.vo.goodsvo.GoodsVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 副本控制器
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-8-26 上午10:10:10
 */
@Controller
public class UndercityController {

	/**
	 * 顯示副本
	 * 
	 * 
	 * @param session
	 * @param scriptId
	 * @return
	 */
	public ResponseMessage show(Session session, int scriptId) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Script script = Context.getCongfigServer().getScript(scriptId);
		int type = Integer.parseInt(script.getParameters());

		Collection<Undercity> undercities = Context.getUndercityServer().getuUndercities();
		List<Map<String, Object>> undercityList = new ArrayList<Map<String, Object>>();
		for (Undercity e : undercities) {
			if (e.getLevel() > player.getLevel() || e.getLevelMax() < player.getLevel()) {
				continue;
			}
			if (e.getType() == type) {
				Map<String, Object> city = new HashMap<String, Object>();
				city.put("id", e.getId());
				city.put("name", e.getName());
				city.put("icon", e.getIcon());
				city.put("level", e.getLevel());
				city.put("levelMax", e.getLevelMax());
				city.put("type", e.getType());

				int times = e.getTimes();
				if (e.getType() == Undercity.typeSingle) {
					if (player.getKnight() != null) {
						times += player.getKnight().getSignleUndercityTimes();
					}
				}
				city.put("times", times);
				int enterTimes = player.getDataInfo().getEveryDay().getUndercityTimes(e.getId());
				city.put("enterTimes", enterTimes);
				undercityList.add(city);

			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("undercityList", undercityList);
		map.put("passUndercitys", player.getDataInfo().getUnders());

		QQDC.getInstance().logClick(player, LogFieldType.undercity);
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 上古秘境
	 * @param session
	 * @return
	 */
	public ResponseMessage famShow(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		int anePass = player.getDataInfo().intDataGet(ConstantsIntKey.anePass);
		Collection<Undercity> undercities = Context.getUndercityServer().getuUndercities();
		Set<Integer> todayIds = new HashSet<Integer>();
		for (Undercity e : undercities) {
			if (e.getType() != Undercity.typeFamAne) {
				continue;
			}
			int enterTimes = player.getDataInfo().getEveryDay().getUndercityTimes(e.getId());
			if (enterTimes > 0) {
				todayIds.add(e.getId());
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("anePass", anePass);
		map.put("todayIds", todayIds);
		return ResponseMessage.getSuccMessage(map);
	}	

	/**
	 * 进入副本
	 * 
	 * @return
	 */
	public ResponseMessage enterUndercity(Session session, int undercityId, int difficulty) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player.stateContains(Constants.playerState256_dart)) {// 押镖状态，不能进入副本
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.dart"));
		}
		if (player.stateContains(Constants.playerState64_market)) {// 摆摊状态，不能进入副本
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.martket"));
		}
		if (player.stateContains(Constants.playerState1024_aiUndercity)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.ai.err"));
		}
		Scene s = Context.getSceneServer().getSceneById(player.getSceneId());
		if (s == null) {
			return null;
		}
		if (s.getType() == Constants.CITY_WALL) {// 塞外场景不能进入副本
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.wallcity"));
		}
		Undercity undercity = Context.getUndercityServer().getUndercity(undercityId);
		if (undercity == null) {
			return null;
		}
		int ucType = undercity.getType();
		Team team = player.getTeam();
		if (team == null) {
			if (undercity.getPersonType() == 1) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.type.team"));
			}
		} else if (undercity.getPersonType() == 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.type.signl"));
		} else if (team.isMaster(playerId) == false) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("team.control.notmaster"));
		}

		int enterSceneId = undercity.getEnterSceneId();
		Scene scene = Context.getSceneServer().getSceneById(enterSceneId);
		if (scene == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
		}
		if (scene.getType() != Constants.CITY_UNDER) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.nocity"));
		}

		if (player.getDataInfo().getUndercityId() > 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.haveenter"));
		}

		if (undercity.getLevel() > player.getLevel() || (undercity.getLevelMax() > 0 && undercity.getLevelMax() < player.getLevel())) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.level"));
		}
		if (ucType == Undercity.typeUnion && player.getUnionId() == 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.errunion"));
		}
		int vipBuyEnterNum = 0;
		if (ucType == Undercity.typePicCheckpoint) {
			int sortLevel = player.getDataInfo().intDataGet(ConstantsIntKey.picCpPass);
			if (sortLevel > 0 && undercity.getSortLevel() > sortLevel) {
				Undercity uc = Context.getUndercityServer().getUndercity(sortLevel, 1);
				return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.notreach1", uc.getName()));
			}

		} else if (ucType == Undercity.typeDrama) {
			int undercityDreamId = player.getDataInfo().intDataGet(ConstantsIntKey.dreamId);
			undercityDreamId = undercityDreamId == 0 ? 1000 : undercityDreamId;
			if (undercity.getId() != undercityDreamId + 1) {
				return null;
			}
		} else if (ucType == Undercity.typeFamAne) {
			int anePass = player.getDataInfo().intDataGet(ConstantsIntKey.anePass);
			anePass = anePass == 0 ? 500 : anePass;
			if (undercity.getId() > anePass + 1) {
				return null;
			}
		} else if (ucType == Undercity.typeSoldierCheckpoint) {
			int soldierLevel = player.getDataInfo().intDataGet(ConstantsIntKey.soldierCpPass);

			if ((difficulty % 5 != 0 || difficulty % 10 != 1) && (difficulty % 5 == 0 && difficulty > (soldierLevel / 10 + 1) * 10)
					&& (difficulty % 10 == 1 && soldierLevel < difficulty)) {
				// 没有达到请求的层
				return null;
			}
			if (difficulty % 5 == 0) {
				int priceIndex = (difficulty % 10 == 0) ? 1 : 0;
				if (player.getGoldAll() < ConstantsModel.undercitySoldierJumpPrice[priceIndex]) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
				}
			}
			enterSceneId = undercity.getSceneIdsArr()[difficulty - 1];
			vipBuyEnterNum = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.ucSCpBuyEn);
		} else if (ucType == Undercity.typeSingle) {
			if (player.getKnight() != null) {
				vipBuyEnterNum += player.getKnight().getSignleUndercityTimes();
			}
		} else if (ucType == Undercity.typeSpalc) {
			if (undercityId == 902) {
				vipBuyEnterNum += player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.gemBuy);
			}else if (undercityId == 903) {//西施副本
				if (player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.tbg) < 5) {
					vipBuyEnterNum = -1;
				}
			}
		}

		boolean isPic = ucType == Undercity.typePicCheckpoint;
		if ((isPic && ConstantsModel.undercityPicCpEnterLimit <= player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.ucPCpEn))
				|| (!isPic && undercity.getTimes() + vipBuyEnterNum <= player.getDataInfo().getEveryDay().getUndercityTimes(undercityId))) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.times"));
		}

		if (team != null) {
			for (long memberId : team.getMemberIds()) {
				if (memberId == playerId) {
					continue;
				}
				Player memberPlayer = Context.getPlayerServer().getByPlayerId(memberId);
				if (memberPlayer.getSceneId() != player.getSceneId()) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.memberposition", memberPlayer.getPlayerName()));

				}
				if (memberPlayer.getDataInfo().getUndercityId() > 0) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.memberhaveenter", memberPlayer.getPlayerName()));
				}
				if (undercity.getLevel() > memberPlayer.getLevel() || (undercity.getLevelMax() > 0 && undercity.getLevelMax() < memberPlayer.getLevel())) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.memberlevel", memberPlayer.getPlayerName()));
				}

				if (undercity.getTimes() <= memberPlayer.getDataInfo().getEveryDay().getUndercityTimes(undercityId)) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.membertimes", memberPlayer.getPlayerName()));
				}

				if (ucType == Undercity.typeUnion && memberPlayer.getUnionId() != player.getUnionId()) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.memberunion", memberPlayer.getPlayerName()));
				}

			}

		}

		PlayerUndercity playerUndercity = Context.getUndercityServer().initPlayerUnderCity(undercityId, enterSceneId, player);
		if (playerUndercity == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
		}

		player.getDataInfo().setUndercityId(playerUndercity.getUndercityId());
		player.getDataInfo().setLastSceneId(player.getSceneId());
		if (isPic) {
			// player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.challengePicCPItem);
			player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.ucPCpEn, 1);
		} else {
			if (undercity.getType() != Undercity.typeDrama && undercity.getType() != Undercity.typeFamAne) {
				player.getDataInfo().getEveryDay().underCityTimesIncrement(undercityId);
			}
			if (ucType == Undercity.typeSoldierCheckpoint) {
				// 加到每日奖励
				player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.challengeSCpItem);
				player.getDataInfo().alterExpRet(Constants.expRet_sbz);// 参加活动标记
			}
		}

		if (ucType == Undercity.typeSingle) {
			Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark15UndercitySingle, 1);
		} else if (ucType == Undercity.typeTeam) {
			Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark16UndercityTeam, 1);
		} else if (ucType == Undercity.typeSoldierCheckpoint) {
			Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark32SBZ, 1);
		}

		Object membersStrObject = null;
		if (team != null) {
			membersStrObject = team.getMemberIds().toString();

			for (Long memberId : team.getMemberIds()) {
				if (team.isMaster(memberId) == false) {
					Player memberPlayer = Context.getPlayerServer().getByPlayerId(memberId);
					memberPlayer.getDataInfo().setUndercityId(playerUndercity.getUndercityId());
					memberPlayer.getDataInfo().setLastSceneId(player.getSceneId());
					memberPlayer.getDataInfo().getEveryDay().underCityTimesIncrement(undercityId);
					if (ucType == Undercity.typeSingle) {
						Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark15UndercitySingle, 1);
					} else if (ucType == Undercity.typeTeam) {
						Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark16UndercityTeam, 1);
					}

					LogServer.recordLog(memberPlayer, ConstantsLog.battle, 40004, player.getDataInfo().getEveryDay().getUndercityTimes(undercityId),
							undercity.getName(), membersStrObject);
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("sceneId", enterSceneId);
		map.put("undercityId", playerUndercity.getUndercityId());
		map.put("enterSuc", 1);
		map.put("exitSceneId", undercity.getExitSceneId());
		if (ucType == Undercity.typeSoldierCheckpoint && player.getKnightTrueLevel() >= ConstantsModel.undercityScpVipLevel && difficulty >= 5) {
			// 神兵阵Vip4跳关获得经验
			map.put("exp", Context.getUndercityServer().addScpExp(playerId, difficulty, 0));
		}

		if (difficulty % 5 == 0) {
			int priceIndex = (difficulty % 10 == 0) ? 1 : 0;
			player.alterGoldAll(-ConstantsModel.undercitySoldierJumpPrice[priceIndex]);
			LogServer.costEventLog(playerId, ConstantsModel.undercitySoldierJumpPrice[priceIndex], ConstantsCost.sbzJump);
			player.commitSync();
		}
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());

		// 添加”人物-进入副本“日志
		LogServer.recordLog(player, ConstantsLog.battle, 40004, player.getDataInfo().getEveryDay().getUndercityTimes(undercityId), undercity.getName(),
				membersStrObject);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 神兵跳关
	 * 
	 * @param difficulty
	 * @return
	 */
	public ResponseMessage jumpScene(Session session, int difficulty) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerUndercity pc = Context.getUndercityServer().getPlayerUndercity(player.getPlayerId());
		if (pc == null || pc.getUndercity() == null) {
			return null;
		}
		Undercity undercity = pc.getUndercity();
		if (undercity.getType() != Undercity.typeSoldierCheckpoint) {
			return null;
		}
		int soldierLevel = player.getDataInfo().intDataGet(ConstantsIntKey.soldierCpPass);

		if ((difficulty % 5 != 0 || difficulty % 10 != 1) && (difficulty % 5 == 0 && difficulty > (soldierLevel / 10 + 1) * 10)
				&& (difficulty % 10 == 1 && soldierLevel < difficulty)) {
			// 没有达到请求的层
			return null;
		}

		int enterSceneId = undercity.getSceneIdsArr()[difficulty - 1];
		if (enterSceneId > player.getSceneId() + 5) {// 只允许跳5关
			return null;
		}
		if (enterSceneId <= player.getSceneId()) {// 不能回跳
			return null;
		}
		int costGold = ConstantsModel.undercitySoldierJumpPrice[0];

		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-costGold);
		player.commitSync();
		player.setTs(enterSceneId);// 传送的标志

		pc.openSecne(enterSceneId);
		LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.sbzJump);
		LogServer.recordLog(player, ConstantsLog.battle, 40201, costGold, difficulty, player.getSceneId(), enterSceneId);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sceneId", enterSceneId);
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());

		if (player.getKnightTrueLevel() >= ConstantsModel.undercityScpVipLevel && difficulty >= 10) {
			// 神兵阵Vip4场景内跳关获得经验
			map.put("exp", Context.getUndercityServer().addScpExp(session.getUid(), difficulty, difficulty - 5));
		}
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 打开机关
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage openBody(Session session) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		PlayerUndercity playerUndercity = Context.getUndercityServer().getPlayerUndercity(playerId);
		int sceneId = player.getSceneId();
		Scene scene = Context.getSceneServer().getSceneById(sceneId);
		if (scene.isCityUndercity() == false) {
			return null;
		}
		if (scene.getOpenKey() != Constants.sceneKeyOpenBody && scene.getOpenKey() != Constants.sceneKeyClearOrBody) {
			return null;
		}
		int goodsId = scene.getOpenParam1();
		int goodsId2 = scene.getOpenParam2();
		goodsId2 = goodsId2 < 100 ? 0 : goodsId2;

		PlayerBoxs playerBoxs = Context.getGoodsServer().getPlayerBoxs(playerId);
		if (playerBoxs.getStackNum(goodsId) < 1 || (goodsId2 > 0 && playerBoxs.getStackNum(goodsId2) < 1)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.openbody.paraless"));
		}
		StringBuffer logSB = new StringBuffer();
		playerBoxs.costGoods(goodsId, 1);
		logSB.append(Context.getGoodsServer().getModeById(goodsId).getName());
		if (goodsId2 > 0) {
			playerBoxs.costGoods(goodsId2, 1);
			logSB.append(",").append(Context.getGoodsServer().getModeById(goodsId2).getName());
		}
		Team team = player.getTeam();
		if (team != null) {
			Collection<Player> allPlayers = team.getAllPlayers();
			for (Player e : allPlayers) {
				if (e.getPlayerId() != playerId) {
					e.getPlayerBoxs().costGoods(goodsId, 1);
					if (goodsId2 > 0) {
						e.getPlayerBoxs().costGoods(goodsId2, 1);
					}
				}
			}
		}
		if (playerUndercity != null) {
			playerUndercity.passScene(sceneId, 0);
		}
		
		// 添加”战斗-打开机关“日志
		LogServer.recordLog(player, ConstantsLog.battle, 40005, scene.getName(), logSB.toString());

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 离开副本
	 * 
	 * @return
	 */
	public ResponseMessage exitUndercity(Session session, int undercityId) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		PlayerUndercity puc = null;
		puc = Context.getUndercityServer().getPlayerUndercity(playerId);

		if (puc == null) {
			return null;
		}
		Undercity uc = puc.getUndercity();
		// 挑战通天图失败退出弹出提示
		Map<String, Object> map = new HashMap<String, Object>();
		if (uc.getType() == Undercity.typePicCheckpoint || uc.getType() == Undercity.typeSoldierCheckpoint) {
			map.put("pass", puc.isPassThroug());
		}
		Context.getUndercityServer().gcUndercity(playerId);
		int lastSceneId = player.getDataInfo().getLastSceneId();

		Team team = player.getTeam();
		if (team != null && team.isMaster(playerId)) {
			for (long memberId : team.getMemberIds()) {
				if (memberId != playerId) {
					Context.getUndercityServer().gcUndercity(memberId);
					Player memberPlayer = Context.getPlayerServer().getByPlayerId(memberId);
					if (memberPlayer.getSceneId() == player.getSceneId()) {
						memberPlayer.getDataInfo().setUndercityId(0);
					}
				}
			}
		}
		player.getDataInfo().setUndercityId(0);

		map.put("exitSuc", 1);
		map.put("sceneId", lastSceneId);

		undercityId = uc.getId();

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 检查远古塞外是否开启
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage checkWallTime(Session session) {
		return ResponseMessage.getSuccMessage(Context.getSceneServer().checkIsWallTime());
	}

	/**
	 * 进入塞外场景
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage intoWallScene(Session session) {

		if (!Context.getSceneServer().checkIsWallTime()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("scene.wall.time"));
		}

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < ConstantsLevel.lv42Wall) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("scene.wall.level", ConstantsLevel.lv42Wall));
		}
		if (player.stateContains(Constants.playerState1024_aiUndercity)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.ai.err"));
		}
		ModeSceneWall.addPlayer(player, ConstantsModel.wallSceneIds[0]);
		ModeSceneWall.checkWallEndTask();
		player.getDataInfo().setLastSceneId(player.getSceneId());
		if (player.getDataInfo().getExpCoef() > 0 && Context.getSceneServer().getWallBeginTime().getTime() / 1000 - 600 >= player.getDataInfo().getExpTime()) {
			player.getDataInfo().setExpCoef(0);// 修正上次的属性
			player.getDataInfo().setExpTime(0);
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("sceneId", ConstantsModel.wallSceneIds[0]);
		resultMap.put("endTime", TimeProcesser.getUnixTime(Context.getSceneServer().getWallEndTime()));
		resultMap.put("expBuff", player.getDataInfo().getExpCoef());
		resultMap.put("expGold", ConstantsModel.wallExpOnceGold);
		// 加到每日奖励
		player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.enterWallSceneItem);
		player.getDataInfo().alterExpRet(Constants.expRet_wall);// 参加活动标记
		QQDC.getInstance().logClick(player, LogFieldType.warSaiwai);
		return ResponseMessage.getSuccMessage(resultMap);
	}

	/**
	 * 塞外使用经验加倍
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage wallSceneBuff(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Scene scene = Context.getSceneServer().getSceneById(player.getSceneId());
		if (scene == null || !scene.isCityWall()) {
			return null;
		}
		ModeSceneWall wall = ModeSceneWall.getPlayer(player.getPlayerId());
		if (wall == null) {// 未进入过塞外不可使用
			return null;
		}
		int expCoefNow = player.getDataInfo().getExpCoef();
		if (expCoefNow >= 50) {
			return null;
		}
		if (player.getGoldAll() < ConstantsModel.wallExpOnceGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-ConstantsModel.wallExpOnceGold);
		player.commitSync();

		expCoefNow += 10;
		player.getDataInfo().setExpTime(TimeProcesser.getUnixTime());
		player.getDataInfo().setExpCoef(expCoefNow);
		LogServer.costEventLog(session.getUid(), ConstantsModel.wallExpOnceGold, ConstantsCost.wallExp);
		LogServer.recordLog(player, ConstantsLog.gold, 50005, ConstantsModel.wallExpOnceGold, expCoefNow);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("expBuff", expCoefNow);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 获取通天图或神兵阵闯关排行
	 * 
	 * @param session
	 * @param sortLevel
	 *            第几关 0:默认请求自己当前关的排行和信息 非0:请求关排行信息
	 * @param type
	 *            1 通天图 2 神兵阵
	 * @return
	 */
	public ResponseMessage getPassThoughRanking(Session session, int sortLevel, int type) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (type != 1 && type != 2) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		boolean reqPrivate = false;
		if (sortLevel == 0) {
			reqPrivate = true;
			String sortLevelKey = ConstantsIntKey.picCpPass;
			if (type == 2) {
				sortLevelKey = ConstantsIntKey.soldierCpPass;
			}
			sortLevel = player.getDataInfo().intDataGet(sortLevelKey);
			if (sortLevel == 0) {// 初始第一关
				sortLevel = 1;
				player.getDataInfo().intDataSet(sortLevelKey, sortLevel);
				player.getDataInfo().commitSync();
			}
		}

		Undercity undercity = Context.getUndercityServer().getUndercity(sortLevel, type);
		if (undercity == null || (undercity.getType() != Undercity.typePicCheckpoint && undercity.getType() != Undercity.typeSoldierCheckpoint)) {
			return null;
		}

		Collection<PassThroughRecordVO> records = Context.getUndercityServer().getRankings(undercity.getId(), ConstantsModel.undercityRankingNum);
		if (type == 1) {
			map.put("ranking", records);
			SysModeAwards awards = SysModeAwards.get(undercity.getAwardId());
			map.put("awards", Context.getGoodsServer().makeAwards(awards.getSubPopsList()));
			if (reqPrivate) {
				map.put("passThroughNum", sortLevel);
				map.put("wasteNum", player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.ucPCpEn));
				List<Map<String, Object>> ucList = new LinkedList<Map<String, Object>>();
				for (int ucId : ConstantsModel.undercityPicCpIds) {
					Undercity uc = Context.getUndercityServer().getUndercity(ucId);
					Map<String, Object> cp = new HashMap<String, Object>();
					cp.put("id", uc.getId());
					cp.put("sortLevel", uc.getSortLevel());
					cp.put("level", uc.getLevel());
					cp.put("name", uc.getName());
					cp.put("boss", Context.getCacheCenter().getMonsterById(ConstantsModel.underPicCpBossIds[uc.getSortLevel() - 1]).getName());
					ucList.add(cp);
				}
				map.put("ucList", ucList);
			}
			QQDC.getInstance().logClick(player, LogFieldType.sbz);
		} else if (type == 2) {
			int index = 0;
			if (sortLevel > 1 && sortLevel <= ConstantsModel.undercitySoldierMax) {
				index = (sortLevel - 1) / 10;
			} else if (sortLevel > ConstantsModel.undercitySoldierMax) {
				index = (ConstantsModel.undercitySoldierMax - 1) / 10;
			}
			List<GoodsVO> bossDropGoodsVoList = new LinkedList<GoodsVO>();
			for (int goodsId : ConstantsModel.underSoldierCheckPointBossDrop[index]) {
				bossDropGoodsVoList.add(new GoodsVO(Context.getGoodsServer().getModeById(goodsId)));
			}
			map.put("bossDropGoodsVoList", bossDropGoodsVoList);
			if (reqPrivate) {
				map.put("ranking", records);
				map.put("revivedNum", player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.revivedNum));
				map.put("passThroughNum", sortLevel);
				map.put("wasteNum", player.getDataInfo().getEveryDay().getUndercityTimes(undercity.getId()));
				map.put("extralEnterBuy", player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.ucSCpBuyEn));
			}
			QQDC.getInstance().logClick(player, LogFieldType.ttt);
		}
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * Vip玩家购买额外的神兵阵进入次数
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage buyExtralEnter(Session session) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player.getKnightTrueLevel() < ConstantsModel.undercityScpVipLevel) {
			return null;
		}
		int buyNum = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.ucSCpBuyEn);
		if (buyNum >= ConstantsModel.undercitySoldierCpBuyNum[player.getKnightLevel() - ConstantsModel.undercityScpVipLevel]) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.buy.numlimit"));
		}
		int price = ConstantsModel.undercitySoldierCpEnterPrice[buyNum];
		if (player.getGoldAll() < price) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.buy.goldlack"));
		}
		player.alterGoldAll(-price);
		player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.ucSCpBuyEn, 1);
		player.commitSync();
		player.getDataInfo().commitSync();

		// 添加”通天图、神兵阵过关奖励“日志
		LogServer.recordLog(player, ConstantsLog.goods, 70006, ConstantsModel.undercitySoldierCpEnterPrice);
		LogServer.costEventLog(playerId, price, ConstantsCost.sbzTimes);
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 领取通天图或神兵阵过关奖励
	 * 
	 * @param session
	 * @param undercityId
	 *            * @param goodsIdStr要的物品goodsId
	 * @return
	 */
	public ResponseMessage getPassReward(Session session, String goodsIdStr) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		PlayerUndercity puc = Context.getUndercityServer().getPlayerUndercity(playerId);
		if (puc == null || puc.getUndercity() == null) {
			return null;
		}
		Undercity uc = puc.getUndercity();
		boolean isPicCp = uc.getType() == Undercity.typePicCheckpoint;
		// boolean isSoldierCp = uc.getType() ==
		// Undercity.typeSoldierCheckpoint;
		if (!isPicCp /* && !isSoldierCp */) {
			return null;
		}

		int sortLevel = player.getDataInfo().intDataGet(ConstantsIntKey.picCpPass);
		if (uc.getSortLevel() < sortLevel - 1 || (sortLevel > ConstantsModel.undercityPicCpIds.length && puc.getRewardId() <= 0)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.reward.alread"));
		}

		if (puc.getRewardId() <= 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.reward.notpass"));
		}

		List<Integer> goodsId = StringProcesser.toIntList(goodsIdStr);
		SysModeAwards awards = SysModeAwards.get(uc.getAwardId());
		List<SubPops> goods = new LinkedList<SubPops>();
		if (goodsId != null) {
			for (SubPops e : awards.getSubPopsList()) {
				for (int gid : goodsId) {
					if (gid == e.getGoodsId()) {
						goods.add(e);
					}
				}
			}
		}
		if (!player.getPlayerBoxs().checkBlack(goods.size())) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.player.boxmin", goods.size()));
		}
		StringBuffer logSB = new StringBuffer();
		Context.getGoodsServer().giveSubPops(goods, player, logSB);
		player.commitSync();
		// 添加”通天图、神兵阵过关奖励“日志
		LogServer.recordLog(player, ConstantsLog.battle, 40007, logSB.toString());
		puc.setRewardId(0);

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 神兵阵原地复活
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage revive(Session session) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		PlayerUndercity playerUndercity = Context.getUndercityServer().getPlayerUndercity(playerId);
		if (playerUndercity == null || playerUndercity.getUndercity().getType() != Undercity.typeSoldierCheckpoint || !playerUndercity.isDead()) {
			return null;
		}
		int revivedNum = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.revivedNum);
		boolean isVip = player.getKnightTrueLevel() >= ConstantsModel.undercityScpVipLevel;
		if ((!isVip && revivedNum >= ConstantsModel.undercityScpReviveNum) || (isVip && revivedNum >= ConstantsModel.undercityScpVip4RevivedNum)) {
			return ResponseMessage.getFailMessage(false);
		}
		if (isVip && revivedNum >= ConstantsModel.undercityScpReviveNum) {
			if (player.getGoldAll() < ConstantsModel.undercityScpRevivePrice) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.revive.lackgold"));
			}
			player.alterGoldAll(-ConstantsModel.undercityScpRevivePrice);
			player.commitSync();
			// 添加“ 神兵阵复活“日志
			LogServer.recordLog(player, ConstantsLog.battle, 40008, ConstantsModel.undercityScpRevivePrice);

			LogServer.costEventLog(playerId, ConstantsModel.undercityScpRevivePrice, ConstantsCost.sbzRevive);
		}
		player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.revivedNum, 1);
		player.getDataInfo().commitSync();
		playerUndercity.revive();
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("hp", player.getHp());
		map.put("mp", player.getMp());
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		return ResponseMessage.getSuccMessage(map);
	}

	@AccessClose
	public ResponseMessage buySpecialTimes(Session session, int underId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (underId != 902) {
			return null;
		}
		int haveTimes = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.gemBuy);
		int costGold = Context.getUndercityServer().getBuyCost(haveTimes + 1);
		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-costGold);
		player.getDataInfo().getEveryDay().intDataSet(ConstantsDayIntKey.gemBuy, haveTimes + 1);

		player.commitSync();

		LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.specialTimes);
		LogServer.recordLog(player, ConstantsLog.battle, 40026, costGold, player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.gemBuy));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("costGold", Context.getUndercityServer().getBuyCost(haveTimes + 2));

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 进入副本后购买buff
	 * 
	 * @param session
	 * @param underId
	 * @return
	 */
	public ResponseMessage buySpecialBuff(Session session, int underId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerUndercity playerUndercity = Context.getUndercityServer().getPlayerUndercity(session.getUid());
		if (playerUndercity == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("unsercity.err.noin"));
		}
		if (playerUndercity.getUndercityId() != underId) {
			return null;
		}
		if (underId != 902) {
			return null;
		}
		int powerCoef = playerUndercity.getPowerCoef();
		if (powerCoef >= 300) {
			return null;
		}
		if (player.getGoldAll() <= ConstantsModel.underBuffGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-ConstantsModel.underBuffGold);
		powerCoef += 50;
		playerUndercity.setPowerCoef(powerCoef);

		LogServer.costEventLog(session.getUid(), ConstantsModel.underBuffGold, ConstantsCost.specialBuff);
		LogServer.recordLog(player, ConstantsLog.battle, 40027, ConstantsModel.underBuffGold);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("powerCoef", powerCoef);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 购买双倍掉率
	 * 
	 * @param session
	 * @param underId
	 * @return
	 */
	public ResponseMessage buySpecialGotRate(Session session, int underId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerUndercity playerUndercity = Context.getUndercityServer().getPlayerUndercity(session.getUid());
		if (playerUndercity == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.err.noin"));
		}
		if (playerUndercity.isRateDouble()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.err.ratehave"));
		}
		if (playerUndercity.getUndercityId() != underId) {
			return null;
		}
		if (underId != 902) {
			return null;
		}
		int enterTimes = player.getDataInfo().getEveryDay().getUndercityTimes(underId);
		int costGold = Context.getUndercityServer().getBuyRateCost(enterTimes);
		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-costGold);
		player.commitSync();
		playerUndercity.setRateDouble(true);// 设置副本爆率双倍

		LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.specialGotRate);
		LogServer.recordLog(player, ConstantsLog.battle, 40028, costGold);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rateDouble", true);
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 显示我完成的剧情编号
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage showUndercityDreamId(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int undercityDreamId = player.getDataInfo().intDataGet(ConstantsIntKey.dreamId);
		undercityDreamId = undercityDreamId == 0 ? 1000 : undercityDreamId;
		undercityDreamId++;

		QQDC.getInstance().logClick(player, LogFieldType.dreamList);
		return ResponseMessage.getSuccMessage(undercityDreamId);
	}

	public ResponseMessage showUndercityDream(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int undercityDreamId = player.getDataInfo().intDataGet(ConstantsIntKey.dreamId);
		undercityDreamId = undercityDreamId == 0 ? 1000 : undercityDreamId;
		undercityDreamId++;
		int key = (undercityDreamId - 1000) / 9;
		if ((undercityDreamId - 1000) % 9 != 0) {
			key++;
		}
		int aUnderDream = player.getDataInfo().intDataGet(ConstantsIntKey.aUnderDream);
		int nextAward = aUnderDream + 1;
		if (nextAward > 11) {// 11次全领取了
			nextAward = 0;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rootId", key);
		map.put("nextAward", nextAward);
		map.put("isOk", key > nextAward);
		map.put("isOver", nextAward == 0);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 领取
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage undercityDreamAward(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int undercityDreamId = player.getDataInfo().intDataGet(ConstantsIntKey.dreamId);
		undercityDreamId = undercityDreamId == 0 ? 1000 : undercityDreamId;
		undercityDreamId++;
		int key = (undercityDreamId - 1000) / 9;
		if ((undercityDreamId - 1000) % 9 != 0) {
			key++;
		}
		int aUnderDream = player.getDataInfo().intDataGet(ConstantsIntKey.aUnderDream);
		int nextAward = aUnderDream + 1;
		if (nextAward > 11) {
			return null;
		}
		if (key <= nextAward) {// 未完成
			return null;
		}

		StringBuffer logSB = new StringBuffer();
		SysModeAwards sysAwards = SysModeAwards.get(123, nextAward);
		if (sysAwards != null) {
			List<SubPops> subPops = sysAwards.getSubPopsList();
			int black = Context.getGoodsServer().checkSubPopsBlack(subPops);
			if (!player.getPlayerBoxs().checkBlack(black)) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.player.boxmin", black));
			}
			Context.getGoodsServer().giveSubPops(subPops, player, logSB);
		}
		LogServer.recordLog(player, ConstantsLog.activity, 90009, nextAward, logSB.toString());

		player.getDataInfo().intDataSet(ConstantsIntKey.aUnderDream, nextAward);
		player.getDataInfo().commitSync();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("suc", "true");
		map.put("asset", player.getAsset());
		map.put("exp", player.getExp());

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 副本挂机
	 * 
	 * @param session
	 * @param undercityId
	 * @return
	 */
	public ResponseMessage aiUndercity(Session session, int undercityId, int times) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		boolean isPass = player.getDataInfo().isPassUnder(undercityId);
		if (!isPass) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.ai.nopass"));
		}
		if (player.stateContains(Constants.playerState1024_aiUndercity)) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.ai.isdoning"));
		}
		if (times <= 0) {
			return null;
		}
		Scene scene = Context.getSceneServer().getSceneById(player.getSceneId());
		if (!scene.isCityMain()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.ai.noscene"));
		}
		Undercity undercity = Context.getUndercityServer().getUndercity(undercityId);
		if (undercity == null) {
			return null;
		}
		if (undercity.getType() != Undercity.typeSingle && undercity.getType() != Undercity.typeUnion) {
			return null;
		}
		if (player.getTeam() != null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.type.signl"));
		}
		Scene enterScene = Context.getSceneServer().getSceneById(undercity.getEnterSceneId());
		if (enterScene == null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
		}
		if (player.getDataInfo().getUndercityId() > 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.haveenter"));
		}

		if (undercity.getLevel() > player.getLevel() || (undercity.getLevelMax() > 0 && undercity.getLevelMax() < player.getLevel())) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.enter.level"));
		}

		int enterTimes = undercity.getTimes();

		if (player.getKnight() != null) {
			enterTimes += player.getKnight().getSignleUndercityTimes();
		}
		int leaveTimes = enterTimes - player.getDataInfo().getEveryDay().getUndercityTimes(undercityId);
		if (leaveTimes < times) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("undercity.ai.havetimes", leaveTimes));
		}
		player.stateAdd(Constants.playerState1024_aiUndercity);
		// 启动挂机任务
		AiUndercityTask aiTask = new AiUndercityTask(player.getPlayerId(), undercityId, times);
		Context.getDataServer().aiTaskAdd(player.getPlayerId(), aiTask);
		aiTask.runBoos();
		LogServer.recordLog(player, ConstantsLog.battle, 40017, undercity.getName(), times);

		QQDC.getInstance().logClick(player, LogFieldType.aibattle);

		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 显示挂机界面
	 * 
	 * @param session
	 * @param step
	 * @return
	 */
	public ResponseMessage aiShow(Session session, int step) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		AiUndercityTask aiTask = Context.getDataServer().aiTaskGet(player.getPlayerId());
		if (aiTask == null) {
			return ResponseMessage.getFailMessage(false);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", aiTask.getStartTime());
		map.put("endTime", aiTask.getEndTime());
		map.put("delay", AiUndercityTask.delay);
		map.put("aiTimes", aiTask.getAiTimes());
		map.put("cStep", aiTask.getStep());
		map.put("cTimes", aiTask.getTimes());
		map.put("nowStep", aiTask.getNowstep());

		map.put("suc", aiTask.isSuc());
		map.put("step", step);

		List<AiReport> aiReports = aiTask.getAiReports();
		if (step == 0) {
			map.put("aiReports", aiReports);
		} else {
			if (aiReports != null && aiReports.size() >= step) {
				map.put("currentReports", aiReports.get(step - 1));
			} else {
				map.put("currentReports", false);
			}
		}

		map.put("aiUnderGold", ConstantsModel.aiUnderGold);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 加速挂机
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage aiFastComplete(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		AiUndercityTask aiTask = Context.getDataServer().aiTaskGet(player.getPlayerId());
		if (aiTask == null || aiTask.isSuc()) {
			return ResponseMessage.getFailMessage(false);
		}
		Undercity undercity = Context.getUndercityServer().getUndercity(aiTask.getUndercityId());
		if (undercity == null) {
			return ResponseMessage.getFailMessage(false);
		}
		int leaveTime = aiTask.getEndTime() - TimeProcesser.getUnixTime();

		int costGold = NumberProcesser.ceil(leaveTime, 60) * ConstantsModel.aiUnderGold;
		if (costGold > player.getGoldAll()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		if (costGold <= 0) {
			return null;
		}
		player.alterGoldAll(-costGold);
		LogServer.costEventLog(player.getPlayerId(), costGold, ConstantsCost.aiFast);

		aiTask.cancel();
		aiTask.runFast();

		player.stateRemove(Constants.playerState1024_aiUndercity);
		player.commitSync();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());

		LogServer.recordLog(player, ConstantsLog.battle, 40019, undercity.getName(), leaveTime, costGold);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 取消挂机
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage aiCancel(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (!player.stateContains(Constants.playerState1024_aiUndercity)) {
			return ResponseMessage.getFailMessage(false);
		}
		AiUndercityTask aiTask = Context.getDataServer().aiTaskGet(player.getPlayerId());
		if (aiTask == null || aiTask.isSuc()) {
			return ResponseMessage.getFailMessage(false);
		}
		aiTask.cancel();
		Context.getDataServer().aiTaskRemove(player.getPlayerId());
		player.stateRemove(Constants.playerState1024_aiUndercity);
		Undercity undercity = Context.getUndercityServer().getUndercity(aiTask.getUndercityId());
		LogServer.recordLog(player, ConstantsLog.battle, 40018, undercity.getName(), aiTask.getTimes(), aiTask.getStep());
		return ResponseMessage.getSuccMessage(true);
	}

	public ResponseMessage aiClose(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		AiUndercityTask aiTask = Context.getDataServer().aiTaskGet(player.getPlayerId());
		if (aiTask == null || !aiTask.isSuc()) {
			return ResponseMessage.getFailMessage(false);
		}
		Context.getDataServer().aiTaskRemove(player.getPlayerId());
		player.stateRemove(Constants.playerState1024_aiUndercity);

		return ResponseMessage.getSuccMessage(true);
	}

}
